package com.hencoder.hencoderpracticedraw1.practice;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.hencoder.hencoderpracticedraw1.R;

public class Practice10HistogramView extends View {
    private Paint paint,paint1;
    private int w;
    private int h;

    public Practice10HistogramView(Context context) {
        super(context);
    }

    public Practice10HistogramView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public Practice10HistogramView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.w=w;
        this.h=h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        综合练习
//        练习内容：使用各种 Canvas.drawXXX() 方法画直方图
        float centerW=w/2;
        float centerH=h/2;
        paint=new Paint();
        paint1=new Paint();
        paint.setColor(getResources().getColor(R.color.colorAccent));
        paint1.setColor(getResources().getColor(R.color.white));
        paint1.setStyle(Paint.Style.STROKE);
        RectF rectF=new RectF(centerW-438,centerH-328,centerW+438,centerH+328);
        canvas.drawRect(rectF,paint1);

        Path path1=new Path();
        path1.moveTo(centerW-400,centerH-300);
        path1.lineTo(centerW-400,centerH+200);
        path1.rLineTo(800,0);
        canvas.drawPath(path1,paint1);

        Path path=new Path();
        float w1=centerW-386;
        float h1=centerH+180;
        RectF rectF1=new RectF(centerW-400+14,centerH+180,centerW-300+14,centerH+200);
        RectF rectF2=new RectF(w1+114,centerH+100,w1+214,centerH+200);
        RectF rectF3=new RectF(w1+(114*2),centerH,w1+(114*2)+100,centerH+200);
        RectF rectF4=new RectF(w1+(114*3),centerH-50,w1+(114*3)+100,centerH+200);
        RectF rectF5=new RectF(w1+(114*4),centerH-100,w1+(114*4)+100,centerH+200);
        path.addRect(rectF1, Path.Direction.CCW);
        path.addRect(rectF2, Path.Direction.CCW);
        path.addRect(rectF3, Path.Direction.CCW);
        path.addRect(rectF4, Path.Direction.CCW);
        path.addRect(rectF5, Path.Direction.CCW);
        canvas.drawPath(path,paint);

        float textWidth=paint1.measureText("java");

        canvas.drawText("java",w1+(100-textWidth)/2,centerH+220,paint1);

    }
}
